﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DynORM.Interfaces;
using DynORM.Mappers;
using DynORM.Enums;
using DynORM;
using DynORM.ValueObjects;
using DynORM.ReferenceObjects;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace UnitTests.DAL
{
    [TestClass]
    public class EqualitySemanticsUnitTests
    {
        private FieldDescriptor GenerateNewFieldDescriptor(Type type, string name)
        {
            return new FieldDescriptor(type, name);
        }

        private IFieldDescriptor GenerateNewIFieldDescriptor(Type type, string name)
        {
            return new FieldDescriptor(type, name);
        }


        [TestMethod]
        public void FieldDescriptorEqualitySemanticsTest()
        {
            FieldDescriptor fd1 = GenerateNewFieldDescriptor(typeof(Guid), "Id");
            FieldDescriptor fd2 = GenerateNewFieldDescriptor(typeof(Guid), "Id");
            bool equal = (fd1 == fd2) ? true : false;
            Assert.IsTrue(equal);
        }

        [TestMethod]
        public void FieldDescriptorEqualitySemanticsTest2()
        {
            FieldDescriptor fd1 = GenerateNewFieldDescriptor(typeof(Guid), "Id1");
            FieldDescriptor fd2 = GenerateNewFieldDescriptor(typeof(Guid), "Id2");
            bool equal = (fd1 == fd2) ? true : false;
            Assert.IsTrue(!equal);
        }

        [TestMethod]
        public void IFieldDescriptorEqualitySemanticsTest()
        {
            IFieldDescriptor fd1 = GenerateNewIFieldDescriptor(typeof(Guid), "Id");
            IFieldDescriptor fd2 = GenerateNewIFieldDescriptor(typeof(Guid), "Id");
            bool equal = (fd1.Equals(fd2)) ? true : false;
            Assert.IsTrue(equal);
            bool equal1 = ((FieldDescriptor)fd1 == (FieldDescriptor)fd2) ? true : false;
            Assert.IsTrue(equal1);
        }


        private Field GenerateNewField(Type type, string name, object value)
        {
            return new Field(type, name, value);
        }

        [TestMethod]
        public void FieldEqualitySemanticsTest()
        {
            Field f1 = GenerateNewField(typeof(Int32), "Id1", 1);
            Field f2 = GenerateNewField(typeof(Int32), "Id2", 1);
            bool equal = (f1 == f2) ? true : false;
            Assert.IsTrue(!equal);
        }

        [TestMethod]
        public void FieldEqualitySemanticsTest2()
        {
            Field f1 = GenerateNewField(typeof(Int32), "Id1", 1);
            Field f2 = GenerateNewField(typeof(Int32), "Id2", 2);
            bool equal = (f1 == f2) ? true : false;
            Assert.IsTrue(!equal);
        }


        private PrimaryKeyDescriptor GenerateNewPrimaryKeyDescriptor(string tablename, string fieldname1, string fieldname2)
        {
            IFieldDescriptor fd1 = GenerateNewFieldDescriptor(typeof(Guid), fieldname1);
            IFieldDescriptor fd2 = GenerateNewFieldDescriptor(typeof(Guid), fieldname2);
            Dictionary<int, IFieldDescriptor> fds = new Dictionary<int, IFieldDescriptor>();
            fds.Add(0, fd1);
            fds.Add(1, fd2);
            return new PrimaryKeyDescriptor(tablename, fds, tablename+"_PK");
        }

        [TestMethod]
        public void PrimaryKeyDescriptorEqualitySemanticsTest()
        {
            PrimaryKeyDescriptor pk1 = GenerateNewPrimaryKeyDescriptor("Test1", "RowID_1", "RowID_2");
            PrimaryKeyDescriptor pk2 = GenerateNewPrimaryKeyDescriptor("Test1", "RowID_1", "RowID_2");
            bool equal = (pk1 == pk2) ? true : false;
            Assert.IsTrue(equal);
        }

        [TestMethod]
        public void PrimaryKeyDescriptorEqualitySemanticsTest2()
        {
            PrimaryKeyDescriptor pk1 = GenerateNewPrimaryKeyDescriptor("Test1", "RowID_1", "RowID_2");
            PrimaryKeyDescriptor pk2 = GenerateNewPrimaryKeyDescriptor("Test2", "RowID_1", "RowID_100");
            bool equal = (pk1 == pk2) ? true : false;
            Assert.IsTrue(!equal);
        }

        private PrimaryKey GenerateNewPrimaryKey(string tablename, string fieldname1, string fieldname2, object value1, object value2)
        {
            IField f1 = GenerateNewField(typeof(Guid), fieldname1, value1);
            IField f2 = GenerateNewField(typeof(Guid), fieldname2, value2);
            Dictionary<int, IField> fs = new Dictionary<int, IField>();
            fs.Add(0, f1);
            fs.Add(1, f2);
            return new PrimaryKey(tablename, fs, tablename + "_PK");
        }

        [TestMethod]
        public void PrimaryKeyEqualitySemanticsTest()
        {
            Guid id1 = Guid.NewGuid();
            Guid id2 = Guid.NewGuid();
            PrimaryKey pk1 = GenerateNewPrimaryKey("Test1", "RowID_1", "RowID_2", id1, id2);
            PrimaryKey pk2 = GenerateNewPrimaryKey("Test1", "RowID_1", "RowID_2", id1, id2);
            bool equal = (pk1 == pk2) ? true : false;
            Assert.IsTrue(equal);
        }

        [TestMethod]
        public void PrimaryKeyEqualitySemanticsTest2()
        {
            Guid id1 = Guid.NewGuid();
            Guid id2 = Guid.NewGuid();
            PrimaryKey pk1 = GenerateNewPrimaryKey("Test1", "RowID_1", "RowID_2", id1, id2);
            PrimaryKey pk2 = GenerateNewPrimaryKey("Test1", "RowID_1", "RowID_2", id2, id1);
            bool equal = (pk1 == pk2) ? true : false;
            Assert.IsTrue(!equal);
        }

        private ForeignKeyDescriptor GenerateNewForeignKeyDescriptor(string tablename, string reltablename, string fieldname1, string fieldname2)
        {
            IFieldDescriptor f1 = GenerateNewFieldDescriptor(typeof(Guid), fieldname1);
            IFieldDescriptor f2 = GenerateNewFieldDescriptor(typeof(Guid), fieldname2);
            Dictionary<int, IFieldDescriptor> fds = new Dictionary<int, IFieldDescriptor>();
            fds.Add(0, f1);
            fds.Add(1, f2);
            return new ForeignKeyDescriptor(tablename, fds, reltablename, fds, tablename + "_" + reltablename + "_FK");
        }

        [TestMethod]
        public void ForeignKeyDescriptorEqualitySemanticsTest()
        {
            ForeignKeyDescriptor fk1 = GenerateNewForeignKeyDescriptor("Test1", "RelTest1", "RowID_1", "RowID_2");
            ForeignKeyDescriptor fk2 = GenerateNewForeignKeyDescriptor("Test1", "RelTest1", "RowID_1", "RowID_2");
            bool equal = (fk1 == fk2) ? true : false;
            Assert.IsTrue(equal);
        }

        [TestMethod]
        public void ForeignKeyDescriptorEqualitySemanticsTest2()
        {
            ForeignKeyDescriptor fk1 = GenerateNewForeignKeyDescriptor("Test1", "RelTest1", "RowID_1", "RowID_2");
            ForeignKeyDescriptor fk2 = GenerateNewForeignKeyDescriptor("Test1", "RelTest1", "RowID_1", "RowID_100");
            bool equal = (fk1 == fk2) ? true : false;
            Assert.IsTrue(!equal);
        }

        private ForeignKey GenerateNewForeignKey(string tablename, string reltablename, string fieldname1, string fieldname2, object value1, object value2)
        {
            IField f1 = GenerateNewField(typeof(Guid), fieldname1, value1);
            IField f2 = GenerateNewField(typeof(Guid), fieldname2, value2);
            Dictionary<int, IField> fs = new Dictionary<int, IField>();
            fs.Add(0, f1);
            fs.Add(1, f2);
            return new ForeignKey(tablename, fs, reltablename, fs, tablename + "_" + reltablename + "_FK");
        }

        [TestMethod]
        public void ForeignKeyEqualitySemanticsTest()
        {
            Guid id1 = Guid.NewGuid();
            Guid id2 = Guid.NewGuid();
            ForeignKey fk1 = GenerateNewForeignKey("Test1", "RelTest1", "RowID_1", "RowID_2", id1, id2);
            ForeignKey fk2 = GenerateNewForeignKey("Test1", "RelTest1", "RowID_1", "RowID_2", id1, id2);
            bool equal = (fk1 == fk2) ? true : false;
            Assert.IsTrue(equal);
        }

        [TestMethod]
        public void ForeignKeyEqualitySemanticsTest2()
        {
            Guid id1 = Guid.NewGuid();
            Guid id2 = Guid.NewGuid();
            ForeignKey fk1 = GenerateNewForeignKey("Test1", "RelTest1", "RowID_1", "RowID_2", id1, id2);
            ForeignKey fk2 = GenerateNewForeignKey("Test1", "RelTest1", "RowID_1", "RowID_2", id2, id1);
            bool equal = (fk1 == fk2) ? true : false;
            Assert.IsTrue(!equal);
        }
    }
}
